using System;
using UnityEngine;

public class DragToRotate : MonoBehaviour
{
    private Vector3 previousMousePosition;
    public bool isFocus;

    private void OnMouseDown()
    {
        isFocus = true;
    }

    private void OnMouseUp()
    {
        isFocus = false;
    }

    void Update()
    {
        
        if(!isFocus)return;
        if (Input.GetMouseButtonDown(0))
        {
            previousMousePosition = Input.mousePosition;
        }
        else if (Input.GetMouseButton(0))
        {
            Vector3 currentMousePosition = Input.mousePosition;
            Vector3 delta = currentMousePosition - previousMousePosition;

            // 根据鼠标移动的距离进行旋转
            float rotationSpeed = 0.5f;
            transform.Rotate(Vector3.up, -delta.x * rotationSpeed, Space.World);
            transform.Rotate(Vector3.right, delta.y * rotationSpeed, Space.Self);

            previousMousePosition = currentMousePosition;
        }
    }
}